/**
 * 
 */
package com.kingcore.director.setting.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import wzw.beans.DbBean;
import wzw.swing.table.PlainTable;
import wzw.swing.table.PlainTableModel;
import wzw.util.ComponentUtil;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.ui.DirectorDialog;
import com.kingcore.director.main.MainFrame;
import com.sunking.swing.JNumberField;

/**
 * @author WZWEN
 *
 */
public class ClassConfigDialog extends DirectorDialog 
	implements ItemListener,ActionListener,ListSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel = null;
	private JComboBox lbbh = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField val = null;
	private JLabel jLabel2 = null;
	private JTextField txt = null;
	private JLabel jLabel3 = null;
	private JCheckBox isDefault = null;
	private JTextField orderby = null;
	private JButton btnSave = null;
	private JButton btnDel = null;
	private JScrollPane jScrollPane = null;
	private PlainTable table = null;
	private MainFrame mf = null;
	
	private static final int MainTable_Column_Index_Code = 0;
	private static final int MainTable_Column_Index_Text = 1;
	private static final int MainTable_Column_Index_OrderBy = 2;
	private static final int MainTable_Column_Index_IsDefault = 3;
	
	private static final int MainTable_Column_Count = 4;
	
	public static final String[] MainTable_Headers = {"类别编号","类别名称","显示顺序","默认否"};
	private static final int MainDialog_Size_Width = 430;
	public static final int[] MainTable_Columns_Width = {60, MainDialog_Size_Width-60-60-48, 60, 48};
	
	
	/**
	 * This method initializes lbbh	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getLbbh() {
		if (lbbh == null) {
			lbbh = new JComboBox();
			lbbh.setBounds(new java.awt.Rectangle(80,10,210,25));
			lbbh.addItemListener( this );
		}
		return lbbh;
	}

	/**
	 * This method initializes val	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getVal() {
		if (val == null) {
			val = new JTextField();
			val.setBounds(new java.awt.Rectangle(80,40,80,25));
		}
		return val;
	}

	/**
	 * This method initializes txt	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxt() {
		if (txt == null) {
			txt = new JTextField();
			txt.setBounds(new java.awt.Rectangle(241,40,148,25));
		}
		return txt;
	}

	/**
	 * This method initializes orderby	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getOrderby() {
		if (orderby == null) {
			orderby = new JNumberField(2);	//JTextField
			orderby.setBounds(new java.awt.Rectangle(80,70,80,25));
			orderby.setText("9");
		}
		return orderby;
	}

	/**
	 * This method initializes btnSave	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton();
			btnSave.setBounds(new java.awt.Rectangle(240,70,65,28));
			btnSave.setText("保存");
			btnSave.addActionListener( this );
		}
		return btnSave;
	}

	/**
	 * This method initializes btnDel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton();
			btnDel.setBounds(new java.awt.Rectangle(322,70,65,28));
			btnDel.setText("删除");
			btnDel.addActionListener( this );
		}
		return btnDel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new java.awt.Rectangle(5,103,434,171));
			jScrollPane.setViewportView( getTable() );
		}
		return jScrollPane;
	}

	/**
	 * This method initializes table	
	 * 
	 * @return javax.swing.JTable	
	 */
	private PlainTable getTable() {
		if (table == null) {
			table = new PlainTable();
			
			DefaultTableModel dtm = new DefaultTableModel(1, MainTable_Column_Count); //no new methods
		    PlainTableModel sorter = new PlainTableModel( dtm, table.getTableHeader());
		    table.setModel(sorter);
			table.setShowGrid(true);
		    
			table.getModel().getTableModel().setColumnIdentifiers( MainTable_Headers );
			table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
			table.showZebraCross();

			table.getSelectionModel().addListSelectionListener( this );

			//设置宽度
			TableColumn tc = null;
			TableColumnModel tcm = table.getColumnModel(); 
	        for(int i=0; i <MainTable_Columns_Width.length; i++)   {
	            tc   =   tcm.getColumn(i);
	            tc.setPreferredWidth(MainTable_Columns_Width[i]); 
	            
	        }
		}
		return table;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
	}

	/**
	 * This is the default constructor
	 */
	public ClassConfigDialog() {
		this(null);
	}

	public ClassConfigDialog(MainFrame frame) {
		super(frame, true);
		this.mf = frame;
		initialize();
		
		try {
			fecthData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private boolean fecthData() throws SQLException {
		ComponentUtil.initComboBox( lbbh, "Select Val,Txt,isDefault From "+DirectorConstants.SYS_Class+" Where LBBH='000000' Order By orderby asc");
		String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
		ComponentUtil.initTable( getTable(),"Select Val,Txt,Orderby,isDefault From "+DirectorConstants.SYS_Class+" Where LBBH='"+ts_lbbh+"' Order By orderby asc", false );
		
		return true;
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(450, 309);
		this.setResizable(false);
		this.setTitle("基本类别初始");
		this.setContentPane(getMainPanel());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getMainPanel() {
		if (mainPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new java.awt.Rectangle(17,70,85,23));
			jLabel3.setText("显示顺序：");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new java.awt.Rectangle(180,40,77,23));
			jLabel2.setText("分类名称：");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new java.awt.Rectangle(17,40,85,23));
			jLabel1.setText("分类代码：");
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(17,10,85,23));
			jLabel.setText("当前类别：");
			mainPanel = new JPanel();
			mainPanel.setLayout(null);
			mainPanel.add(getLbbh(), null);
			mainPanel.add(jLabel, null);
			mainPanel.add(jLabel1, null);
			mainPanel.add(getVal(), null);
			mainPanel.add(jLabel2, null);
			mainPanel.add(getTxt(), null);
			mainPanel.add(jLabel3, null);
			mainPanel.add(getOrderby(), null);
			mainPanel.add(getBtnSave(), null);
			mainPanel.add(getBtnDel(), null);
			mainPanel.add(getJScrollPane(), null);
			mainPanel.add(getIsDefault(), null);
		}
		return mainPanel;
	}

	public JCheckBox getIsDefault() {
		if (isDefault == null) {
			isDefault = new JCheckBox();
			isDefault.setBounds(new java.awt.Rectangle(176,70,70,23));
			isDefault.setText("设默认");
		}
		return isDefault;
	}
	/**
	 * implements ItemListener
	 */
	public void itemStateChanged(ItemEvent e) {
		Object src = e.getSource();
		if(src==getLbbh()){
			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			ComponentUtil.initTable( getTable(),"Select Val,Txt,Orderby,isDefault From "+DirectorConstants.SYS_Class+" Where LBBH='"+ts_lbbh+"' Order By orderby asc", false );
			
		}
	}

	/**
	 * implements ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		if( src==btnSave ){
			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			String ts_val  = getVal().getText();
			String ts_txt  = getTxt().getText();
			String ts_order= getOrderby().getText();
			String ts_isDefault = getIsDefault().isSelected()?"1":"0";
			if( ts_lbbh==null ){
				JOptionPane.showMessageDialog(this,"没有类别编号！");
				lbbh.requestFocus();
				return;
			}
			if( ts_val==null ){
				JOptionPane.showMessageDialog(this,"请填写分类代码！");
				val.requestFocus();
				return;
			}
			if( ts_txt==null ){
				JOptionPane.showMessageDialog(this,"请填写分类名称！");
				txt.requestFocus();
				return;
			}
			Vector<String> fv_sql=new Vector<String>();
			String sql="Delete From "+DirectorConstants.SYS_Class+" Where LBBH='"+ts_lbbh+"' And VAL='"+ts_val+"'";	//删除不抛出异常,drop抛出。
			fv_sql.add(sql);
			
			if (ts_isDefault.equals("1")) {
				sql="Update "+DirectorConstants.SYS_Class+" set isDefault=0 Where LBBH='"+ts_lbbh+"'";
			}
			fv_sql.add(sql);
			
			sql="Insert Into "+DirectorConstants.SYS_Class+"(LBBH,VAL,TXT,isDefault,ORDERBY) " +
					"Values('"+ts_lbbh+"','"+ 
							   ts_val+"','"+
							   ts_txt+"',"+
							   ts_isDefault+","+
							   ts_order+")";
			fv_sql.add(sql);
	
			Boolean isAddNew = true;
			DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
			try {
				dbBean.executeBatch( fv_sql );
				Vector<String> v_row = new Vector<String>();
				v_row.add(ts_val);
				v_row.add(ts_txt);
				v_row.add(ts_order);
				v_row.add(ts_isDefault);
				int ti_row = table.getRowCount();
				for (int i = 0; i < ti_row; i++) {
					if( table.getValueAt(i, MainTable_Column_Index_Code).equals(ts_val)){
						isAddNew = false;
						table.setValueAt(ts_txt, i, MainTable_Column_Index_Text);
						table.setValueAt(ts_order, i, MainTable_Column_Index_OrderBy);
						table.setValueAt(ts_isDefault, i, MainTable_Column_Index_IsDefault);
						//((DefaultTableModel)table.getModel()).removeRow( i );
						//ti_row -- ;
					}else{
						// set other's default to '0'
						if(ts_isDefault.equals("1")) {
							table.setValueAt("0", i, MainTable_Column_Index_IsDefault);
						}
							
					}
				}
				
				if (isAddNew) {
					table.getModel().getTableModel().addRow(v_row);
				}
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
			
		} else if( src==btnDel ){
			if(JOptionPane.showConfirmDialog(this,"确认删除当前信息吗？","提示",JOptionPane.OK_CANCEL_OPTION)
					!=JOptionPane.OK_OPTION)
				return;
			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			int row = getTable().getSelectedRow();
			if(row<0){
				JOptionPane.showMessageDialog(this,"请选择要删除的行！");
				return ;
			}
			String ts_val  = getTable().getValueAt( row, MainTable_Column_Index_Code).toString();

			String sql="Delete From "+DirectorConstants.SYS_Class +
					" Where LBBH='"+ts_lbbh+"' "+ 
					  " And Val ='"+ts_val+"'";
			DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
			try {
				dbBean.executeUpdate( sql );
				table.getModel().getTableModel().removeRow( row );
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
		}
		
	}

	/**
	 * implements ListSelectionListener.
	 */
	public void valueChanged(ListSelectionEvent arg0) {
	    int fi_row=table.getSelectedRow();
	    if(fi_row<0)
	    	return;
	    getVal().setText( table.getValueAt( fi_row, MainTable_Column_Index_Code).toString() );
	    getTxt().setText( table.getValueAt( fi_row, MainTable_Column_Index_Text).toString() );
	    getOrderby().setText( table.getValueAt( fi_row, MainTable_Column_Index_OrderBy).toString() );
	    getIsDefault().setSelected( table.getValueAt( fi_row, MainTable_Column_Index_IsDefault).toString().equals("1")?true:false );
	    
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"


